package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;
    @Autowired
    private LabelInfoService labelInfoService;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoService roomInfoService;

    @Override
    @Transactional
    public void saveOrUpdate1(ApartmentSubmitVo apartmentSubmitVo) {

        Long apartmentId = apartmentSubmitVo.getId();

        if (apartmentId != null) {
            //todo 删除配套
            LambdaQueryWrapper<ApartmentFacility> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper1.eq(ApartmentFacility::getApartmentId, apartmentId);
            apartmentFacilityService.remove(lambdaQueryWrapper1);
//            todo  删除标签
            LambdaQueryWrapper<ApartmentLabel> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper2.eq(ApartmentLabel::getApartmentId, apartmentId);
            apartmentLabelService.remove(lambdaQueryWrapper2);
//            todo  删除杂费
            LambdaQueryWrapper<ApartmentFeeValue> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper3.eq(ApartmentFeeValue::getApartmentId, apartmentId);
            apartmentFeeValueService.remove(lambdaQueryWrapper3);
//            todo  删除图片
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper4 = new LambdaQueryWrapper<>();
            lambdaQueryWrapper4.eq(GraphInfo::getItemId, apartmentId)
                    .eq(GraphInfo::getItemType, ItemType.APARTMENT);
            graphInfoService.remove(lambdaQueryWrapper4);
        }

        Long provinceId = apartmentSubmitVo.getProvinceId();
        ProvinceInfo provinceInfo = provinceInfoService.getById(provinceId);
        apartmentSubmitVo.setProvinceName(provinceInfo.getName());

        Long cityId = apartmentSubmitVo.getCityId();
        CityInfo cityInfo = cityInfoService.getById(cityId);
        apartmentSubmitVo.setCityName(cityInfo.getName());

        Long districtId = apartmentSubmitVo.getDistrictId();
        DistrictInfo districtInfo = districtInfoService.getById(districtId);
        apartmentSubmitVo.setDistrictName(districtInfo.getName());

        this.saveOrUpdate(apartmentSubmitVo);
        apartmentId = apartmentSubmitVo.getId();

        //todo 添加配套
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if (facilityInfoIds != null) {
            List<ApartmentFacility> apartmentFacilityList = new ArrayList<>(facilityInfoIds.size());
            for (Long facilityInfoId : facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .apartmentId(apartmentId)
                        .facilityId(facilityInfoId).build();
                apartmentFacilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }
        //todo 添加标签
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        if ( ! labelIds.isEmpty()) {
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>(labelIds.size());
            for (Long labelId : labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                        .apartmentId(apartmentId)
                        .labelId(labelId)
                        .build();
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }
        //todo 添加杂费
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (feeValueIds != null) {
            List<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>(feeValueIds.size());
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                        .apartmentId(apartmentId)
                        .feeValueId(feeValueId)
                        .build();
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }
        //todo 添加图片
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if (graphVoList != null) {
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setItemId(apartmentId);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
    }

    @Override
    public IPage<ApartmentItemVo> pageList(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        /*LambdaQueryWrapper<ApartmentItemVo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(queryVo.getProvinceId() != null,ApartmentItemVo::getProvinceId,queryVo.getProvinceId())
                .eq(queryVo.getCityId()!=null,ApartmentItemVo::getCityId,queryVo.getCityId())
                .eq(queryVo.getDistrictId()!=null,ApartmentItemVo::getDistrictId,queryVo.getDistrictId());
        */
        IPage<ApartmentItemVo> list = apartmentInfoMapper.pageList(page,queryVo);
        return list;
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = this.getById(id);
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id)
                .eq(GraphInfo::getItemType,ItemType.APARTMENT);
        List<GraphInfo> graphInfoList = graphInfoService.list(graphInfoLambdaQueryWrapper);
        List<GraphVo> graphVoList = new ArrayList<>(graphInfoList.size());
        graphInfoList.forEach(graphInfo -> {
            GraphVo graphVo = new GraphVo();
            BeanUtils.copyProperties(graphInfo,graphVo);
            graphVoList.add(graphVo);
        });
        apartmentDetailVo.setGraphVoList(graphVoList);


        LambdaQueryWrapper<ApartmentLabel> apartmentLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        apartmentLabelLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        List<ApartmentLabel> apartmentLabelList = apartmentLabelService.list(apartmentLabelLambdaQueryWrapper);
        List<LabelInfo> labelInfoList = new ArrayList<>(apartmentLabelList.size());
        apartmentLabelList.forEach(l -> {
            LabelInfo labelInfo = labelInfoService.getById(l.getLabelId());
            labelInfoList.add(labelInfo);
        });
        apartmentDetailVo.setLabelInfoList(labelInfoList);


        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);

        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);

        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomInfoLambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        if(roomInfoService.count(roomInfoLambdaQueryWrapper) > 0){
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //todo 删除配套
        LambdaQueryWrapper<ApartmentFacility> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(ApartmentFacility::getApartmentId, id);
        apartmentFacilityService.remove(lambdaQueryWrapper1);
//            todo  删除标签
        LambdaQueryWrapper<ApartmentLabel> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(ApartmentLabel::getApartmentId, id);
        apartmentLabelService.remove(lambdaQueryWrapper2);
//            todo  删除杂费
        LambdaQueryWrapper<ApartmentFeeValue> lambdaQueryWrapper3 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper3.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(lambdaQueryWrapper3);
//            todo  删除图片
        LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper4 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper4.eq(GraphInfo::getItemId, id)
                .eq(GraphInfo::getItemType, ItemType.APARTMENT);
        graphInfoService.remove(lambdaQueryWrapper4);

        this.removeById(id);
    }

    @Override
    public void updateReleaseStatusById(Long id, ReleaseStatus status) {
        LambdaUpdateWrapper<ApartmentInfo> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(ApartmentInfo::getId,id)
                .set(ApartmentInfo::getIsRelease,status);
        this.update(lambdaUpdateWrapper);
    }

    @Override
    public List<ApartmentInfo> listInfoByDistrictId(Long id) {
        LambdaQueryWrapper<ApartmentInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(ApartmentInfo::getDistrictId,id);
        return this.list(lambdaQueryWrapper);
    }

}




